/* 
    version:1.0.0
    author:chengXuXin
    date:xxxx.xx.xx
    company:xxxx
    .....
 */
(function (global) {
  "use strict"; //严格代码模式(变量必须先声明再使用)

  // 核心模块封装
  // 核心模块如果使用普通函数封装，将不利于后期功能(或属性的扩展)
  // 解决方法：用构造函数(可以通过函数的原型进行扩展)
  // var myFramework = function(){
  //     console.log(11111);
  // }

  var myFramework = function (selector) {
    return new myFramework.core.init(selector);
  };
  // 设置原型(为了后面方便操作原型对象，在这里用cre代替原型对象)
  myFramework.core = myFramework.prototype = {
    version: "1.0.0",
    constructor: myFramework,
    myFramework: myFramework,
  };

  // 真正创建myFramework对象的构造函数
  var init = (myFramework.core.init = function (selector) {
    // console.log("这里是构造函数");

    // 功能模块
    // 创建和获取DOM
    if (!selector) {
      return this;
    } else if (typeof selector === "string") {
      return parseStringToDom(selector, this);
    }
  });

  // 让init和myFramework拥有共同的原型对象
  init.prototype = myFramework.core;

  // 创建和获取DOM函数封装
  // 分析：1）如果传的参数是：<div class = "box" title = "这是一个虚拟的标签">测试</div>,则创建该DOM
  // 2)如果传入的参数是：#box,.box,span,则获取DOM
  function parseStringToDom(selector, target) {
    // 1）如果传的参数是：<div class = "box" title = "这是一个虚拟的标签">测试</div>,则创建该DOM
    // 获取字符串第n个字符并判断其值的三种方法
    // console.log(selector[0] === "<");
    // console.log(selector.charAt(0) === "<");
    // console.log(selector.indexOf("<") === 0);
    // console.log(selector.charAt(selector.length-1)===">");
    if (
      selector.charAt(0) === "<" &&
      selector.charAt(selector.length - 1) === ">" &&
      selector.length >= 3
    ) {
      target[0] = selector;
      target.length = 1;
      // document.write(selector)
      // return selector;
    } else {
      //获取DOM
      // console.log(selector.slice(1,selector.length),target);
      if (selector.charAt(0) == "#") {
        // 传入的是id选择器
        var dom = document.getElementById(selector.slice(1, selector.length));
        // return dom
        target[0] = dom;
        target.length = 1;
      } else if (selector.charAt(0) == ".") {
        // 传入的是class选择器
        var doms = document.getElementsByClassName(
          selector.slice(1, selector.length)
        );
        for (var i in doms) {
          target[i] = doms[i];
        }
        target.length = doms.length;
      } else {
        // 传入的是标签选择器
        var doms = document.getElementsByTagName(selector);
        for (var i in doms) {
          target[i] = doms[i];
        }
        target.length = doms.length;
      }
      return target;
    }
  }
  // 为了便于扩展方法，添加extend来方便后续的操作
  myFramework.extend = myFramework.core.extend = function (obj) {
    for (var key in obj) {
      this[key] = obj[key];
    }
  };

  // 添加扩展方法
  myFramework.core.extend({
    first: function () {
      return this[0];
    },
    last: function () {
      return this[this.length - 1];
    },
    get: function (idx) {
      return this[idx];
    },
    each: function (callback) {
      for (var i = 0; i < this.length; i++) {
        callback(this[i], i, this);
      }
    },
  });
  global.myFramework = global.my = myFramework;

  // 扩展事件
  myFramework.core.extend({
    click: function (callback) {
      this.each(function (item, index) {
        item.onclick = function (event) {
          callback(event);
        };
        // item.onclick = callback;
      });
    },
  });
})(typeof window === "undefined" ? this : window);
